package com.File;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class Directory_ {
    public static void main(String[] args) {

    }

    @Test
    public void m1() {
        String filePath = "d:\\JavaFile";//文件夹也是一种特殊的文件
        File file = new File(filePath);
        if (file.exists()) {
            if (file.delete()) {
                System.out.println("删除成功");
            } else {
                System.out.println("失败");
            }
        } else {
            System.out.println("文件不存在");
        }
    }

    @Test
    public void m2() {
        String directoryPath = "d:\\JavaFile\\";
        File file = new File(directoryPath);
        if (file.exists()) {
            System.out.println("ok");
        } else {
            if (file.mkdirs()) {
                System.out.println("directory");
            } else {
                System.out.println("ERROR");
            }
        }
    }

    @Test
    public void readFile() {
        int read = 0;
        String filePath = "d:\\news.txt";
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            while ((read = fileInputStream.read()) != -1) {
                System.out.print((char) read);//转成char显示
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭文件流释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void readFile01() {
        //字节数组
        byte[] buf = new byte[8];//一次取8个字节
        int readLen = 0;
        String filePath = "d:\\news.txt";
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            while ((readLen = fileInputStream.read(buf)) != -1) {
                System.out.print(new String(buf, 0, readLen));//转成char显示
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭文件流释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Test
    public void writeFile(){
        //创建FileOutputStream对象
        String filePath = "d:\\new.txt";
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream=new FileOutputStream(filePath);//创建方式,当前写入内容是.会覆盖原来的内容
            //fileOutputStream=new FileOutputStream(filePath,true);//追加写
            //写入一个字节
            //fileOutputStream.write('h');
            //写入字符串
            String str = "hello word";
            //fileOutputStream.write(str.getBytes());//srt.getBytes可以把字符串转变为字符数组
            fileOutputStream.write(str.getBytes(),0,str.length());//从第几个字符,到第几个字符
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
